using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Users;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using Common;
using Authentication.User;

public partial class controls_tm_ReceiverList : System.Web.UI.UserControl
{
    public delegate void ProcessSelected(ListItemCollection lb);
    public ListItemCollection SelectedItems
    {
        get 
        {
            //return lbRightList.Items;

            ListItemCollection listItem = new ListItemCollection();
            for (int idx = 0; idx < lbRightList.Items.Count; idx++)
                listItem.Add(lbRightList.Items[idx]);
            return listItem; 
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (lbLeftList.SelectedIndex < 0) return;
        int idx = 0;
        while(idx < lbLeftList.Items.Count)
        {
            if (!lbLeftList.Items[idx].Selected) {idx++; continue;}
            if (!lbRightList.Items.Contains(lbLeftList.Items[idx]))
                lbRightList.Items.Add(lbLeftList.Items[idx]);
            lbLeftList.Items.RemoveAt(idx);
        }

    }
    protected void btnRemove_Click(object sender, EventArgs e)
    {
        if (lbRightList.SelectedIndex < 0) return;
        int idx = 0;
        while (idx < lbRightList.Items.Count)
        {
            if (!lbRightList.Items[idx].Selected) { idx++; continue; }
            if (!lbLeftList.Items.Contains(lbRightList.Items[idx]))
                lbLeftList.Items.Add(lbRightList.Items[idx]);
            lbRightList.Items.RemoveAt(idx);
        }
    }
    
    protected void DoSearch()
    {
        SqlConnection cn = null;
        string account = sysLibs.SecuritySafeString(txtAccount.Text.Trim());
        string name = sysLibs.SecuritySafeString(txtName.Text.Trim());
        string email = sysLibs.SecuritySafeString(txtEmail.Text.Trim());
        string SQLCmd = "", SQLCond= "";
        try
        {
            cn = new SqlConnection(sysLibs.GetConnectionString);
            SQLCmd = " SELECT TOP " + systemSettings.sysMaxNoOfSearchResultInListbox  +
                     " CASE WHEN userType & " + sysLibs.turnBitOn(0, consts.constENTBitPos) + ">0" +
                     " THEN companyName1 ELSE fullName END AS name,userId FROM tm_users ";

            //Enable and enterprise or personal
            int userType =  sysLibs.turnBitOn(0, consts.constENTBitPos) +
                            sysLibs.turnBitOn(0, consts.constPERBitPos);
            SQLCond = " usersStatus ='" + consts.constStatusENABLE + "' AND " +
                      " userType & " + userType + ">0";

            if (account != "")
            {
                SQLCond += (SQLCond == "" ? "" : " AND ") +
                    "(account LIKE N'" + account + consts.constSQLAllWildcard + "')";
            }
            if (name != "")
            {
                SQLCond += (SQLCond == "" ? "" : " AND ") + 
                    "(fullName LIKE N'" + consts.constSQLAllWildcard + name + consts.constSQLAllWildcard + "' OR " +
                    " companyName1 LIKE N'" + consts.constSQLAllWildcard + name + consts.constSQLAllWildcard + "')";
            }
            if (email != "")
            {
                SQLCond += (SQLCond == "" ? "" : " AND ") + 
                          "(email LIKE N'" + consts.constSQLAllWildcard + email + consts.constSQLAllWildcard + "' OR " + 
                          " companyEmail LIKE N'" + consts.constSQLAllWildcard + email + consts.constSQLAllWildcard + "')";
            }
            if (SQLCond != "")  SQLCmd += " WHERE " + SQLCond;
            Common.sysLibs.ShowDebugMsg(lblDebugMessage, SQLCmd);           

            SqlDataAdapter myAdapter = new SqlDataAdapter(SQLCmd, cn);
            DataSet myDataset = new DataSet();
            myAdapter.Fill(myDataset);
            lbLeftList.Items.Clear();  
            lbLeftList.DataSource = myDataset;
            lbLeftList.DataTextField = myDataset.Tables[0].Columns["Name"].ToString();
            lbLeftList.DataValueField = myDataset.Tables[0].Columns["userId"].ToString();
            lbLeftList.DataBind();
        }
        catch (Exception er)
        {
            Common.sysLibs.ShowDebugMsg(lblDebugMessage, er.Message.ToString());
            return;
        }
        finally
        {
            if (cn != null) cn.Close();
        }
    }
    
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        DoSearch();
    }
}
